ui = window.ui ||{
    success:function(message,error){
        var style = (error==1)?"html_clew_box clew_error ":"html_clew_box";
        var html   =   '<div class="" id="ui_messageBox" style="display:none">'
        + '<div class="html_clew_box_close"><a href="javascript:void(0)" onclick="$(this).parents(\'.html_clew_box\').hide()" title="关闭">关闭</a></div>'
        + '<div class="html_clew_box_con" id="ui_messageContent">&nbsp;</div></div>';
        var init      =  0;
		
        var showMessage = function( message ){		
            if( !init ){
                $('body').append( html );
                init = 1;
            }
			

			
            $( '#ui_messageContent' ).html( message );
            $('#ui_messageBox').attr('class',style);
			
            var v =  ui.box._viewport() ;
			
            jQuery('<div class="boxy-modal-blackout"></div>')
            .css(jQuery.extend(ui.box._cssForOverlay(), {
                zIndex: 9999, 
                opacity: 0.2
            })).appendTo(document.body);
			
			
            $( '#ui_messageBox' ).css({
                left:( v.left + v.width/2  - $( '#ui_messageBox' ).outerWidth()/2 ) + "px",
                top:(  v.top  + v.height/2 - $( '#ui_messageBox' ).outerHeight()/2 ) + "px"
            });			
			
            $( '#ui_messageBox' ).fadeIn("slow");
        }
		
		
        var closeMessage = function(){
            setTimeout( function(){  
                $( '#ui_messageBox' ).fadeOut("fast",function(){
                    jQuery('.boxy-modal-blackout:last').remove(); 
                });
            } , 1500);
        }
		
        showMessage( message );
        closeMessage();

    },
	
    error:function(message){
        ui.success(message,1);
    },
    
    ltedit:function(obj,fields,ids){
        var tag = obj.firstChild.tagName;
        if (typeof(tag) != "undefined" && tag.toLowerCase() == "input")
        {
            return;
        }
        /* 保存原始的内容 */
        var org = obj.innerHTML;
        var val = Browser.isIE ? obj.innerText : obj.textContent;

        /* 创建一个输入框 */
        var txt = document.createElement("INPUT");
        txt.value = (val == 'N/A') ? '' : val;
        txt.style.width = (obj.offsetWidth + 12) + "px" ;

        /* 隐藏对象中的内容，并将输入框加入到对象中 */
        obj.innerHTML = "";
        obj.appendChild(txt);
        txt.focus();

        /* 编辑区输入事件处理函数 */
        txt.onkeypress = function(e)
        {
            var evt = Utils.fixEvent(e);
            var obj = Utils.srcElement(e);

            if (evt.keyCode == 13)
            {
                obj.blur();

                return false;
            }

            if (evt.keyCode == 27)
            {
                obj.parentNode.innerHTML = org;
            }
        }

        /* 编辑区失去焦点的处理函数 */
        txt.onblur = function(e)
        {
            if (Utils.trim(txt.value).length > 0)
            {
                $.getJSON(URL+"/_ltedit",{id:ids,field:fields,val:encodeURIComponent(Utils.trim(txt.value))},function(res){
                    if(res.status == 0){
                       // ui.error(res.info);
                       alert(res.info);
                       return;
                    }
                    obj.innerHTML = (res.status == 1) ? res.data : org;
                });
            }
            else
            {
                obj.innerHTML = org;
            }
        }
    },

    load:function(){
        var init = 0
        var loadingBox = '<div class="html_clew_box" id="ui_loading" style="display:none"><div class="html_clew_box_con"><span class="ico_waiting">加载中……</span></div></div>';
        if( !init ){
            $('body').append( loadingBox );
            init = 1;
        }
		
        $( '#ui_loading' ).css({
            right:100+"px",
            top:($(document).scrollTop())+"px"
        });
        $( '#ui_loading' ).fadeIn("slow");
    },
	
    loaded:function(){
        var loadingBox = '#ui_loading';
        $( loadingBox ).fadeOut("slow");
    },
	
    quicklogin:function(){
        ui.box.load( U('home/Public/quick_login') ,{
            title:'快速登录'
        });
    },
	
    sendmessage:function(touid){
        touid = touid || '';
        ui.box.load(U('home/Message/post',['touid='+touid]), {
            title:'发私信'
        });
    },
	
    confirm:function(o,text){
        var callback = $(o).attr('callback');
        text = text || '确定要做此项操作吗？';
        this.html = '<div id="ts_ui_confirm" class="ts_confirm"><dl><a class="del" href="javascript:void(0)" onclick="$(\'.ts_confirm\').remove()"></a><dt class="txt"></dt><dd><input type="button" value="确定"  class="btn_b mr5"><input type="button" value="取消"  class="btn_w"></dd></dl></div>';
        // 修改原因: ts_ui_confirm .btn_b按钮会重复提交
        //if( $('#ts_ui_confirm').html()==null ){
        $('body').append(this.html);
        //}
        var position = $(o).offset();
        $('#ts_ui_confirm').css({
            "top":position.top+"px",
            "left":position.left-($("#ts_ui_confirm").width()/2)+"px",
            "display":"none"
        });
        $("#ts_ui_confirm .txt").html(text);
        $('#ts_ui_confirm').fadeIn("fast");
        $("#ts_ui_confirm .btn_w").one('click',function(){
            $('#ts_ui_confirm').fadeOut("fast");
            // 修改原因: ts_ui_confirm .btn_b按钮会重复提交
            $('#ts_ui_confirm').remove();
        });
        $("#ts_ui_confirm .btn_b").one('click',function(){
            $('#ts_ui_confirm').fadeOut("fast");
            // 修改原因: ts_ui_confirm .btn_b按钮会重复提交
            $('#ts_ui_confirm').remove();
            eval(callback);
        });
    },
	
    emotions:function(o){
        var $talkPop = $('div.talkPop');
        var $body = $('body');
        var $o = $(o);
        if (1 != $talkPop.data('type')) {
            $talkPop.hide();
        }
        this.emotdata = $body.data("emotdata");
        this.html = '<div class="talkPop alL" id="emotions" style="*padding-top:20px;">'
        + '<div style="position: relative; height: 7px; line-height: 3px;z-index:99">'
        + '<img src="' + _THEME_ + '/images/zw_img.gif" style="margin-left: 10px; position: absolute;" class="talkPop_arrow"></div>'
        + '<div class="talkPop_box">'
        + '<div class="close" style="height:30px;line-height:30px;background-color:#F8FAFC;padding:0 10px;position:relative;*width:420px"><a onclick=" $(\'#emotions\').remove()" class="del" href="javascript:void(0)" title="关闭"> </a><span>常用表情</span></div>'
        + '<div class="faces_box" id="emot_content"><img src="'+ _THEME_+'/images/icon_waiting.gif" width="20" class="alM"></div></div></div>';
        target_set = $o.attr('target_set');
        $body.append(this.html);
        var position = $o.offset();
        $('#emotions').css({
            "top":position.top+"px",
            "left":position.left+"px",
            "z-index":99999999
        });
		
        var _this = this;
        if(!this.emotdata){
            $.post( U('home/user/emotions'),{
                target:$(this).attr('target_set')
            } ,function(txt){
                txt = eval('('+txt+')');
                $body.data("emotdata",txt);
                _this.showContent(txt);
            })
        }else{
            _this.showContent(this.emotdata);
        };

        this.showContent = function(data){  //显示表情内容
            var content ='';
            $.each(data,function(i,n){
                content+='<a href="javascript:void(0)" title="'+n.title+'" onclick="ui.emotions_c(\''+n.emotion+'\',\''+target_set+'\')"><img src="'+ _THEME_ +'/images/expression/'+n.type+'/'+ n.filename +'" width="24" height="24" /></a>';
            });
            content+='<div class="c"></div>';
            $('#emot_content').html(content);
        };
		
        $body.live('click',function(event){
            if( $(event.target).attr('target_set')!=target_set ){
                $('#emotions').remove();
            }
        })
    },
	
    emotions_c:function(emot,target){
        $("#"+target).insertAtCaret(emot);
        $("#"+target).focus();
        $('#emotions').remove();
    },	
	
    countNew:function(){
        $.getJSON( U('home/User/countNew') ,function(txt){
            var messageList =  $('.message_list_container');
            var messageSmallDiv = $('#message_list_container');
            var list = {
                comment:{
                    url:U('home/user/comments'),
                    name:"我的评论"
                },
                atme:{
                    url:U('home/user/atme'),
                    name:"新@提到我"
                },
                message:{
                    url:U('home/message/index'),
                    name:"新的私信"
                },
                group_atme:{
                    url:U('group/index/atme'),
                    name:"群内@我的"
                },
                group_comment:{
                    url:U('group/index/comment'),
                    name:"群内评论"
                },
                group_bbs:{
                    url:U('group/index/bbsNotify'),
                    name:"群内帖子消息"
                },
                notify:{
                    url:U('home/message/notify'),
                    name:"系统通知"
                },
                appmessage:{
                    url:U('home/message/appmessage'),
                    name:"系统消息"
                }
            };
            messageList.html("");
            if(txt.total!="0"){
			   
                for(var one in list){
                    if(txt[one] != undefined && parseInt(txt[one]) >0){
                        //<li>两条新消息,<a href="#">查看消息</a></li>
                        var newLi = document.createElement('li');
                        var newNode = document.createTextNode(txt[one]+"条"+list[one].name+"，");
                        var newA = document.createElement('a');
                        newA.href = list[one].url;
                        newA.target = "";
                        newA.innerHTML="查看消息";
                        newA.className="message_list_"+one;
			            
                        newLi.appendChild(newNode);
                        newLi.appendChild(newA);
                        messageList.append(newLi);
			            
                        $('.message_list_'+one).live('click',function(){
                            var className = $(this).attr('class');
                            $.each($('.'+className),function(){
                                $(this).parent().remove();
                            });
                            if(messageList.children('li').size() == 0){
                                messageSmallDiv.hide();
                            }
                            if(className == 'message_list_comment'){
                                $('#app_left_count_comment').html("");
                            }
                            
                            if(className == 'message_list_atme'){
                                $('#app_left_count_atme').html("");
                            }
                            
                        }); 
			            
                    }
                }
                messageList.show();
                messageSmallDiv.show();
            }else{
                messageList.hide();
                messageSmallDiv.hide();
            }
			
            if(txt.comment!="0"){
                $("#app_left_count_comment").html("(<font color=\"red\">"+txt.comment+"</font>)");
            }else{
                $("#app_left_count_comment").html("");
            }
			
            if(txt.atme!="0"){
                $("#app_left_count_atme").html("(<font color=\"red\">"+txt.atme+"</font>)");
            }else{
                $("#app_left_count_atme").html("");
            }
        });
    },
    closeCountList:function(obj){
        $(obj).attr('closed',true);
        $(obj).parent().hide();
    },
    getarea:function(prefix,init_style,init_p,init_c){
        var style = (init_style)?'class="'+init_style+'"':'';
        var html = '<select name="'+prefix+'_province" '+style+'><option>省/直辖市</option></select> <select name="'+prefix+'_city" '+style+'><option value=0>不限</option></select>';
        document.write(html);
        // _PUBLIC_+'/js/area.js'
        $.getJSON(U('home/Public/getArea'), function(json){
            json = json.provinces;
            var province ='<option>省/直辖市</option>';
            $.each(json,function(i,n){
                var pselected='';
                var cselected='';
                var city='<option>不限</option>';
                if(n.id==init_p){
                    pselected = 'selected="true"';
                    $.each(n.citys,function(j,m){
                        for(var p in m){
                            cselected = (p==init_c)?'selected="true"':'';
                            city+='<option value="'+p+'" '+cselected+'>'+m[p]+'</option>';
                        };
                    });
                    $("select[name='"+prefix+"_city']").html(city);
                }
                province+='<option value="'+n.id+'" rel="'+i+'" '+pselected+'>'+n.name+'</option>';
            });
			
            $("select[name='"+prefix+"_province']").live('change',function(){
                var city='<option>不限</option>';
                var handle =  $(this).find('option:selected').attr('rel');
                if( handle ){
                    var t =  json[handle].citys;
                    $.each(t,function(j,m){
                        for(var p in m){
                            city+='<option value='+p+'>'+m[p]+'</option>';
                        };
                    });
                };
                $("select[name='"+prefix+"_city']").html(city);
            });
            $("select[name='"+prefix+"_province']").html(province);
        }); 
    }

	
};

/* *
 * 显示载入信息
 */
function showLoader()
{
    window.parent.document.getElementsByTagName('body').item(0).style.cursor = "wait";
    $(window.parent.document).find('#load-div').show();
}

/* *
 * 隐藏载入信息
 */
function hideLoader()
{
    window.parent.document.getElementsByTagName('body').item(0).style.cursor = "auto";
    $(window.parent.document).find('#load-div').hide();
}


$(document).ajaxStart(showLoader)
    .ajaxComplete(hideLoader);



function sortBy (field,sort){
    location.href = "?_order="+field+"&_sort="+sort;
}

function edit(id){
    var keyValue;
    if (id)
    {
        keyValue = id;
    }else {
        keyValue = getSelectCheckboxValue();
    }
    if (!keyValue)
    {
        alert('请选择编辑项！');
        return false;
    }
    location.href =  URL+"/edit/id/"+keyValue;
}
//获取已选择用户的ID数组
function getChecked() {
    var ids = new Array();
    $.each($('table input:checked'), function(i, n){
        ids.push( $(n).val() );
    });
    return ids;
}
function CheckAll(o){
    if( o.checked == true ){
        $('input[name="checkbox"]').attr('checked','true');
        $('tr[overstyle="on"]').addClass("bg_on");
    }else{
        $('input[name="checkbox"]').removeAttr('checked');
        $('tr[overstyle="on"]').removeClass("bg_on");
    }
}

function checkon(o){
    if( o.checked == true ){
        $(o).parents('tr').addClass('bg_on') ;
    }else{
        $(o).parents('tr').removeClass('bg_on') ;
    }
}
    
function del(ids){
    var length = 0;
    if(ids) {
        length = 1;
    }else {
        ids    = getChecked();
        length = ids[0] == 0 ? ids.length - 1 : ids.length;
        ids    = ids.toString();
    }
    if(ids=='') {
        ui.error('请先选择删除项');
        return ;
    }
    if(confirm('您将删除'+length+'条记录，删除后无法恢复，确定继续？')) {
        $.getJSON(URL+"/delete",{
            id:ids
        },function(res){
            if(res.status==1) {
                ui.success('删除成功');
                removeItem(ids);
            }else {
                ui.error('删除失败');
            }
        });
    }
}
function removeItem(ids) {
    ids = ids.split(',');
    for(i = 0; i < ids.length; i++) {
        $('#'+ids[i]).remove();
    }
}